<template>
  <div style="min-height: 350px">
    <el-form ref="userForm" size="default" :model="entity" :rules="rules" label-width="120px" @submit.native.prevent>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="entity.name" placeholder="姓名" />
      </el-form-item>
      <el-form-item label="企业微信号" prop="wxuid">
        <el-input v-model="entity.wxuid" placeholder="企业微信" />
      </el-form-item>
      <el-form-item label="bpm员工号" prop="cdefine1">
        <el-input v-model="entity.cdefine1" placeholder="bpm员工号" />
      </el-form-item>
      <el-form-item label="考勤员" prop="cdefine2">
        <el-select v-model="entity.cdefine2" placeholder="考勤员" clearable>
          <el-option v-for="item in state.kqyList" :key="item.id" :label="item.cmaker" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="btnSubmit" size="default">确 定</el-button>
        <el-button @click="onCancel" size="default">取 消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup name="userDetail">
import request from '/@/utils/request';
import { ElMessage } from 'element-plus';
import mittBus from '/@/utils/mitt';
import {reactive, ref, onMounted, defineProps, defineEmits, nextTick} from "vue";
import Other from "/@/utils/other";
// 定义变量内容
const userForm = ref(null);
const username = ref(null);

// defineProps(['modelValue'])
const emit = defineEmits(['update:modelValue'])
let props = defineProps({
  modelValue : {type : Boolean, default: true},
  entityId : {type : Number, default: 0},
  orgId : {type : Number, default: 0}
})
let entity = ref({
  username: Other.uuid(),
  pwd : Other.uuid(),
  phone:Other.uuid(),
  name: '',
  wxuid:'',
  cdefine1:'',
  cdefine2:undefined,
  orgId : props.orgId
})
const rules = reactive({
  name: [
    { required: true, message: '请输入姓名', trigger: 'blur' },
    { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
  ],
  wxuid: [
    { required: true, message: '请输入企业微信号', trigger: 'blur' },
    { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur' }
  ],
  cdefine1: [
    { required: true, message: '请输入bpm员工号', trigger: 'blur' },
    { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur' }
  ],
  cdefine2: [
    { required: true, message: '请选择考勤员', trigger: 'blur' },
  ],
})
const state = reactive({
  kqyList :[],
})

const loadEntity = ()=>{
  if(props.entityId > 0){
    request({ url: '/sys/useradmin/getuser', data: {id : props.entityId} }).then( res =>{
      entity.value = res.data
      entity.value.cdefine2 = res.data.cdefine2 ? parseInt(res.data.cdefine2) : undefined
    })
  }
}
const loadKaoQinYuan = ()=>{
  request({
    url: '/kaoqinyuan/listkaoqinyuan',
    data: {}
  }).then(res => {
    const { data } = res
    state.kqyList = data
  })
}
const onCancel = ()=>{
  emit('update:modelValue', false) //关闭弹窗
}
const btnSubmit = ()=>{
  if(!userForm.value) return;
  userForm.value.validate((valid)=>{
    if(valid){
      request({
        url: '/sys/useradmin/addedituser',
        data: entity.value
      }).then( res =>{
        if(res.code === "200"){
          ElMessage.success(res.msg)
          emit('update:modelValue', false) //关闭弹窗
          //触发事件总线，让列表界面更新数据
          props.entityId > 0 ? mittBus.emit('useredit', {}) : mittBus.emit('useradd', {});
        }else{
          ElMessage.error(res.msg);
        }
      })
    }else{
      console.log('error submit!!');
      return false;
    }
  })
}

onMounted(() => {
  loadKaoQinYuan();
  loadEntity();
  nextTick(()=>{
    username.value.focus()
  })
});

</script>

<style scoped>
</style>
